﻿using System.Collections;
using System.Collections.Generic;

namespace Miao.ML
{
	public class ObjectData : GenericReferenceObjectData, IEnumerable<KeyValuePair<string, WinMLData>>, IEnumerable
	{
		internal readonly Dictionary<string, WinMLData> propertyDictionary = new Dictionary<string, WinMLData>();

		public sealed override DataType type => DataType.Object;

		public override bool isNone => propertyDictionary.Count == 0;

		public WinMLData this[string property] => propertyDictionary[property];
		/// <summary>
		/// �������WinML����
		/// </summary>
		/// <param name="property"></param>
		/// <param name="value"></param>
		public void AddProperty(string property, WinMLData value)
		{
			propertyDictionary.Add(property, value);
		}

		public bool RemoveProperty(string property)
		{
			return propertyDictionary.Remove(property);
		}

		public bool TryGetProperty(string property, out WinMLData value)
		{
			return propertyDictionary.TryGetValue(property, out value);
		}

		public override IEnumerable<WinMLData> GetSubData()
		{
			foreach (KeyValuePair<string, WinMLData> item in propertyDictionary)
			{
				yield return item.Value;
			}
		}

		public override string ToString()
		{
			if (base.isNull)
			{
				return string.Format("{0}{1}{2}{3}{4}", base.qualifiedName, '&', "null", '{', '}');
			}
			string text;
			if (isGenericType)
			{
				text = $"{base.qualifiedName}{'<'}";
				foreach (IGeneric genericArgument in genericArgumentList)
				{
					text += $"{genericArgument}{','}";
				}
				text = $"{text.Remove(text.Length - 1, 1)}{'>'}{'&'}{base.referenceID}{'{'}";
			}
			else
			{
				text = $"{base.qualifiedName}{'&'}{base.referenceID}{'{'}";
			}
			if (propertyDictionary.Count > 0)
			{
				foreach (KeyValuePair<string, WinMLData> item in propertyDictionary)
				{
					text += $"{item.Key}={item.Value},";
				}
				text = text.Remove(text.Length - 1, 1);
			}
			return text + "}";
		}

		public IEnumerator<KeyValuePair<string, WinMLData>> GetEnumerator()
		{
			return propertyDictionary.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return propertyDictionary.GetEnumerator();
		}
	}
}
